package com.dxy.df.modules.controller;

import com.dxy.df.core.assit.SearchHandler;
import com.dxy.df.core.model.PageRs;
import com.dxy.df.core.model.Rs;
import com.dxy.df.core.shiro.ShiroUtils;
import com.dxy.df.modules.application.AcceptRiceApplication;
import com.dxy.df.modules.bean.put.UserPublishAcceptRicePut;
import com.dxy.df.modules.bean.query.UserPublishAcceptRiceQuery;
import com.dxy.df.modules.bean.vo.UserAcceptRiceDetailVo;
import com.dxy.df.modules.bean.vo.UserPublishAcceptRiceVo;
import com.dxy.df.modules.service.UserPublishAcceptRiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户收饭信息发布
 *
 * @author biz@dxy.cn
 * @date 2019-10-25 15:36:06
 */
@Api(tags = "用户收饭信息发布")
@RestController
@RequestMapping("/api/users/publish/accept-rice")
public class UserPublishAcceptRiceController {
    @Autowired
    private UserPublishAcceptRiceService userPublishAcceptRiceService;
    @Autowired
    private SearchHandler searchHandler;
    @Autowired
    private AcceptRiceApplication acceptRiceApplication;

    @ApiOperation(value = "用户收饭信息发布分页列表")
    @PostMapping("/search")
    public PageRs<UserPublishAcceptRiceVo> search(@RequestBody @Valid UserPublishAcceptRiceQuery query) {
        //查询列表数据
        List<UserPublishAcceptRiceVo> voList = userPublishAcceptRiceService.queryList(query);
        int total = userPublishAcceptRiceService.queryTotal(query);
        return searchHandler.search(voList, total, query);
    }


    @ApiOperation(value = "用户收饭信息发布获取")
    @GetMapping("")
    public Rs<UserAcceptRiceDetailVo> info() {
        UserPublishAcceptRiceQuery query = new UserPublishAcceptRiceQuery();
        query.setUserId(ShiroUtils.getUserAccount());
        UserAcceptRiceDetailVo userAcceptRiceDetailVo = userPublishAcceptRiceService.queryDetail(query);
        if (null == userAcceptRiceDetailVo) {
            userAcceptRiceDetailVo = new UserAcceptRiceDetailVo();
            userAcceptRiceDetailVo.setBizStatus(0);
        }
        return Rs.ok().putData(userAcceptRiceDetailVo);
    }

    @ApiOperation(value = "用户收饭信息发布新增")
    @PostMapping
    public Rs save(@RequestBody UserPublishAcceptRicePut userPublishAcceptRice) {
        /*LocalTime time = LocalTime.now();
        if (time.getHour() < 16 || time.getHour() > 20) {
            throw new BizException(ErrorEnum.NOT_ACCEPT_RICE_TIME);
        }*/
        acceptRiceApplication.publishAcceptRiceSave(userPublishAcceptRice);
        return Rs.ok();
    }

    @ApiOperation(value = "用户收饭信息发布修改")
    @PutMapping
    public Rs update(@RequestBody UserPublishAcceptRicePut userPublishAcceptRice) {
        userPublishAcceptRiceService.update(userPublishAcceptRice);
        return Rs.ok();
    }

    @ApiOperation(value = "用户收饭信息发布删除")
    @DeleteMapping
    public Rs delete(@RequestParam Integer[] ids) {
        userPublishAcceptRiceService.deleteBatch(ids);
        return Rs.ok();
    }


}
